package com.h3bpm.kumhosunny.crm.controller;

import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Controller.RestfulApi.util.JsonUtils;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import data.DataRow;
import data.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author wjq
 * @Date: 2019/9/26 15:29
 * @Description:出差报告查看详情
 */
@Controller
@RequestMapping({"/Portal/Business"})
public class BusinessViewController extends ControllerBase {

    private static Logger log = LoggerFactory.getLogger(BusinessViewController.class);


    /**
     * 功能描述: 查看出差流程是否已经结束
     *
     * @param:  String ObjectID
     * @return:  status
     * @auther: wjq
     * @date: 2019/9/26 15:33
     */
    @RequestMapping(value = "/getBusinessView", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getBusinessView(String objectId) {
        log.info("  出差流程状态接口=========流程ID:" + objectId);
        if (objectId == null) {
            return "0";
        }
        String querySql = "select State FROM ot_instancecontext WHERE BizObjectId='" + objectId+"'";
        try {
            DataTable dataTable = SqlUtil.doQuery(querySql);
            List<DataRow> entityRows = dataTable.getEntityRows();
            if(entityRows.isEmpty()){
                return  "0";
            }
            DataRow dataRow = entityRows.get(0);
            String string = dataRow.getString(0);
            int status = Integer.parseInt(string);
            log.info("  出差流程状态接口=========流程状态:" +Integer.toString(status));
            return JsonUtils.obj2json(status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * 功能描述: 重定向到出差报告单
     *
     * @param:
     * @return:
     * @auther: Administrator
     * @date: 2019/9/26 16:33
     */
    @RequestMapping(value = "/goBusinessReport", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public  String  goBusinessReport(String objectId,String customerCode ){
        log.info("  出差流程状态接口=========流程ID:" + objectId+"====客户名称"+customerCode );
        if (objectId == null||customerCode == null) {
            return null;
        }

        String querySql="SELECT i.ObjectId FROM i_businesstravelreport i,ot_instancecontext o WHERE i.travelApplInstanceId=o.ObjectId AND o.bizObjectId='"+objectId+"' AND i.customerCode='"+customerCode+"'";
        DataTable dataTable = null;
        try {
            dataTable = SqlUtil.doQuery(querySql);
            List<DataRow> entityRows = dataTable.getEntityRows();
            if(entityRows.isEmpty()){
                log.info("  出差流程跳转接口=========数据为空" );
                return  "0";
            }
            DataRow dataRow = entityRows.get(0);
            //出差报告单对应的objectId
            String string = dataRow.getString(0);
            log.info("  出差流程跳转接口=========报告单ID:" + string );
            return JsonUtils.obj2json(string);
            //return "redirect:/Portal/InstanceSheets.html?InstanceId="+string;


        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;

    }

    @Override
    public String getFunctionCode() {
        return null;
    }
}
